package mq.service.impl;

import com.baomidou.mybatisplus.toolkit.CollectionUtils;
import mq.dao.mapper.MqMessageCumulateMapper;
import mq.domain.MqMessageCumulate;
import mq.service.IMqMessageCumulateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * @description: 消息堆积记录(MqMessageCumulate)表服务实现类
 * @author: zk
 * @since: 2022-08-16 13:39:29
 */
@Slf4j
@Service
public class MqMessageCumulateServiceImpl implements IMqMessageCumulateService {
    @Resource
    private MqMessageCumulateMapper mqMessageCumulateMapper;
//    @Resource
//    private DeductionNoticeManagement deductionNoticeManagement;
//    @Resource
//    private CarDeductionRecordDao carDeductionRecordDao;
//    @Resource
//    private TruckDeductionRecordDao truckDeductionRecordDao;
//    @Resource
//    private TruckDeductionRecordSubMapper truckDeductionRecordSubMapper;
//    @Resource
//    private TruckDeductionRecordMergeMapper truckDeductionRecordMergeMapper;

    @Override
    public void cumulateMessageHandle(String busiNo) {
        log.info("堆积消息补偿处理 start");
        Long preId = null;
        List<Integer> statusList = Arrays.asList(MqMessageCumulate.STATUS_DEFAULT, MqMessageCumulate.STATUS_FAIL);
        List<MqMessageCumulate> mqMessageCumulateList = mqMessageCumulateMapper.queryByStatus(preId, statusList,busiNo, 5000);
        while (CollectionUtils.isNotEmpty(mqMessageCumulateList)) {
            preId = mqMessageCumulateList.get(mqMessageCumulateList.size() - 1).getId();
            MqMessageCumulate mqMessageCumulate = null;
            List<Long> failedIds = new ArrayList<>();
            for (int i = 0; i < mqMessageCumulateList.size(); i++) {
                mqMessageCumulate = mqMessageCumulateList.get(i);
                try {
                    // 先更新状态
                    MqMessageCumulate cumulate = new MqMessageCumulate();
                    cumulate.setId(mqMessageCumulate.getId());
                    cumulate.setStatus(MqMessageCumulate.STATUS_SUCCESS);
                    cumulate.setUpdateTime(new Date());
                    mqMessageCumulateMapper.updateById(cumulate);
                    // 处理
                    handle(mqMessageCumulate);
                } catch (Exception e) {
                    log.error("重新发送失败 returnCallBack={}", mqMessageCumulate, e);
                    failedIds.add(mqMessageCumulate.getId());
                }
            }
            // 批量更新为处理失败
            if (CollectionUtils.isNotEmpty(failedIds)) {
                mqMessageCumulateMapper.updateStatusByIds(failedIds, MqMessageCumulate.STATUS_FAIL);
            }
            mqMessageCumulateList = mqMessageCumulateMapper.queryByStatus(preId, statusList,MqMessageCumulate.BUSI_NO_DEDUCTION, 5000);
        }
        log.info("堆积消息补偿处理 end");
    }

    private void handle(MqMessageCumulate mqMessageCumulate) {
//        DeductionNoticeDto deductionNotice = JSON.parseObject(mqMessageCumulate.getMessage(), DeductionNoticeDto.class);
//        log.info("扣款结果通知处理消息 deductionNotice={}", deductionNotice);
//        // 查询订单
//        OrderNoticeDto orderNoticeDto = getDeductionNotice(deductionNotice.getOrderType(), deductionNotice.getOrderNo(), deductionNotice.getCurrPayChannel());
//        Assert.notNull(orderNoticeDto, "查不到订单信息！orderNo=" + deductionNotice.getOrderNo() + ",orderType=" + deductionNotice.getOrderType());
//        deductionNoticeManagement.notifyAllObservers(orderNoticeDto);
    }

    /**
     * 根据账单类型获取到原账单
     *
     * @param orderType
     * @param orderNo
     * @return
     */
//    public OrderNoticeDto getDeductionNotice(String orderType, String orderNo, Integer payChannel) {
//        switch (orderType) {
//            // 客车
//            case DeductionNoticeDto.ORDER_TYPE_CAR:
//                CarDeductionRecord carDeductionRecord = carDeductionRecordDao.queryByOrderNo(orderNo);
//                return copyProperties(carDeductionRecord, orderType, payChannel);
//            // 货车原账单
//            case DeductionNoticeDto.ORDER_TYPE_TRUCK:
//                TruckDeductionRecord truckDeductionRecord = truckDeductionRecordDao.queryByOrderNo(orderNo);
//                return copyProperties(truckDeductionRecord, orderType, payChannel);
//            // 货车拆分账单
//            case DeductionNoticeDto.ORDER_TYPE_TRUCK_SPLIT:
//                TruckDeductionRecordSub truckDeductionRecordSub = truckDeductionRecordSubMapper.queryByOrderNo(orderNo);
//                return copyProperties(truckDeductionRecordSub, orderType, payChannel);
//            // 货车合并账单
//            case DeductionNoticeDto.ORDER_TYPE_TRUCK_MERGE:
//                TruckDeductionRecordMerge truckDeductionRecordMerge = truckDeductionRecordMergeMapper.queryByOrderNo(orderNo);
//                return copyProperties(truckDeductionRecordMerge, orderType, payChannel);
//        }
//        return null;
//    }

//    private OrderNoticeDto copyProperties(Object obj, String orderType, Integer payChannel) {
//        if (Objects.isNull(obj)) {
//            return null;
//        }
//        OrderNoticeDto orderNoticeDto = new OrderNoticeDto();
//        BeanUtils.copyProperties(obj, orderNoticeDto);
//        // 设置账单类型
//        orderNoticeDto.setOrderType(orderType);
//        // 设置当时的支付渠道
//        orderNoticeDto.setCurrPayChannel(payChannel);
//        return orderNoticeDto;
//    }


}
